var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];


function sortBy(field, reverse, primer) {
    reverse = (reverse) ? -1 : 1;
    return function (a, b) {

        a = a[field];
        b = b[field];

        if (typeof (primer) != 'undefined') {
            a = primer(a);
            b = primer(b);
        }

        if (a < b) return reverse * -1;
        if (a > b) return reverse * 1;
        return 0;
    }
}

function mouseOverHead() {
    var newLink = $(this).css("background-image").replace("_i", "_a");
    $(this).css("background-image", newLink);
}

function mouseOutHead() {
    var newLink = $(this).css("background-image").replace("_a", "_i");
    $(this).css("background-image", newLink);
}

$(function () {
    $("#nav a").mouseover(mouseOverHead);
    $("#nav a").mouseout(mouseOutHead);
});

function getThumbHtml(title, thumbUrl, albumId, numPhotos, updated) {
    template = $("#albumThumbTemplate").html();
    return template.replace("[THUMBURL]", thumbUrl).replace("[ALBUMID]", albumId).replace("[NUMPHOTOS]", numPhotos).replace("[UPDATED]", updated).replace("[TITLE]",title);
}

function getPhotoThumbHtml(thumbUrl,updated,formUrl) {
    template = $("#photoThumbTemplate").html();

    return template.replace("[THUMBURL]", thumbUrl).replace("[FORMURL]", formUrl).replace("[UPDATED]", updated);
}

function getVideoThumbHtml(title, videoUrl, thumbUrl) {
    template = $("#videoThumbTemplate").html();

    return template.replace("[THUMBURL]", thumbUrl).replace("[TITLE]", title).replace("[VIDEOURL]", videoUrl);
}

function loadAlbums(albums) {
    $("#content").html("");
    today = new Date();

    $.each(albums.data.items, function (i, item) {
        title = item.title;
        thumbUrl = item.media.thumbnails[0];
        albumId = item.id;
        numPhotos = item.numPhotos;
        updatedDate = new Date(item.updated);
        monthUpdated = updatedDate.getMonth() + 1;
        dateUpdated = updatedDate.getDate();
        updatedString = "Updated " + dateUpdated + "/" + monthUpdated;

        diff = today - updatedDate;
        daysDifference = Math.round(diff / (1000 * 60 * 60 * 24));

        if (daysDifference < 1)
            updatedString = "<b>Updated today</b>";

        $("#content").append(getThumbHtml(title, thumbUrl, albumId, numPhotos, updatedString));
    });
}

function loadPicture(picture) {
    $("#picture").html("<img src='" + picture.data.media.image.url + "' />");
}

function loadPictures(pictures) {
    $("#content").html("");

    pictures.data.items.sort(sortBy('timestamp', true, parseInt));

    $.each(pictures.data.items, function(i, item) {
        var title = item.title;
        var thumbUrl = item.media.thumbnails[0];
        //var imageUrl = item.media.image.url;
        
        formUrl = 'Picture.aspx?photoId=' + item.id + '&albumId=' + item.album;
        updated = item.updated.substring(0, 10);
        $("#content").append(getPhotoThumbHtml(thumbUrl, updated, formUrl));
    });

    Shadowbox.clearCache(); // <= clear Shadowbox's cache 
    Shadowbox.setup(); // <= set up all Shadowbox links 
}

function loadVideos(videos) {
    $("#content").html("");
    $.each(videos.data.items, function (i, item) {
        var title = item.title;
        var thumbUrl = item.thumbnail.hqDefault;
        var videoUrl = item.content[5] + '&autoplay=1&rel=0';
        updated = item.updated.substring(0, 10);
        $("#content").append(getVideoThumbHtml(title,videoUrl,thumbUrl));
    });

    Shadowbox.clearCache(); // <= clear Shadowbox's cache 
    Shadowbox.setup(); // <= set up all Shadowbox links 
}